﻿// // ******************************************************************
// //       /\ /|       @file       ConfigDataRepository.cs
// //       \ V/        @brief      await29
// //       | "")       @author     Await, 285283010@qq.com
// //       /  |
// //      /  \\        @Modified   2024-12-31 20:12
// //    *(__\_\        @Copyright  Copyright (c) , 2024-11-14 19:11 Await
// // ******************************************************************

using AutomaticSubtitles.Contracts.Vo.Jellyfin;
using AutomaticSubtitles.Service.DataAccess.Model;
using AutomaticSubtitles.Service.Domain.Config;

namespace AutomaticSubtitles.Service.Infrastructure.Repositories;

public class ConfigDataRepository(AutoDbContext context, IUnitOfWork unitOfWork, IHttpClientFactory httpContext, IStringLocalizer<SharedResource> localizer) :
    BaseRepository<AutoDbContext, Auto_Config,
        string>
    (context, unitOfWork, localizer), IConfigDataRepository
{
    /// <summary>
    /// 更新token数据
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    public async Task<string?> UpdateTokenAsync(Auto_Config data)
    {
        var configurationTypeEnum = (GlobalVariableEnumConfiguration.ConfigurationTypeEnum)data.Type;
        switch (configurationTypeEnum)
        {
            case GlobalVariableEnumConfiguration.ConfigurationTypeEnum.Jellyfin:
                var authenticatebyname = await JellyfinHelper.Authenticatebyname(httpContext, new AuthenticatebynameVo()
                {
                    PW = data.Password,
                    UserName = data.UserName
                }, data.ServerUrl, data.Token);
                if (authenticatebyname != null)
                {
                    data.UserId = authenticatebyname.User.Id;
                    context.AutoConfigs.Update(data);
                    await unitOfWork.CommitAsync();
                    return authenticatebyname.User.Id;
                }

                break;
            case GlobalVariableEnumConfiguration.ConfigurationTypeEnum.Emboy:
                break;
            case GlobalVariableEnumConfiguration.ConfigurationTypeEnum.Local:
                break;
            default:
                throw new ArgumentOutOfRangeException(localizer[NoExistent]?.Value ?? "配置错误！");
        }

        return default;
    }
}